package com.lzk.过滤器和拦截器.myInterceptorProject;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Objects;

/**
 * @author: zhongkui 1365693530@qq.com
 * @Date: 2024/11/12
 */
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //记录IP访问次数
        String ip = request.getRemoteAddr();
        //统计访问次数并输出
        HttpSession session = request.getSession();
        Integer count = (Integer) session.getAttribute("count");

        count =  Objects.isNull(count)?1:++count;
        System.out.println("ip:"+ip+"访问了"+count+"次");
        session.setAttribute("count",count);

        //放行
        return true;
    }
}
